package com.lamer.console.base.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lamer.console.base.entity.Role;
import com.lamer.console.base.entity.UserRoleRelation;
import com.lamer.console.base.service.IBaseService;
import com.lamer.console.base.service.IRoleService;
import com.lamer.console.base.service.IUserRoleRelationService;
import com.lamer.console.common.api.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @role 黄诗鹏
 * @since 2021-11-09
 */
@RestController
@RequestMapping("/base")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    @Qualifier("RoleServiceImpl")
    private IBaseService baseService;

    @Autowired
    private IUserRoleRelationService userRoleRelationService;

    @GetMapping("/role/page/")
    public ApiResult<Page<Role>> getPage(@RequestParam("search") String search) {
        JSONObject jsonObject = JSON.parseObject(search);
        Integer number = jsonObject.getInteger("page_number");
        Integer size = jsonObject.getInteger("page_size");
        String roleName = jsonObject.getString("roleName");

        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(roleName)) queryWrapper.like(Role::getRoleName, roleName);

        Page<Role> page = roleService.page(new Page<>(number, size), queryWrapper);
        return ApiResult.success(page);
    }

    @GetMapping("/role/{id}")
    public ApiResult<Role> getData(@PathVariable("id") int id) {
        Role data = roleService.getById(id);
        return data == null ? ApiResult.failed() : ApiResult.success(data);
    }

    @PostMapping("/role/")
    public ApiResult<Role> insertData(@RequestBody Role data){
        boolean flag = roleService.save(data);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @PutMapping("/role/")
    public ApiResult<Role> updateData(@RequestBody Role data){
        boolean flag = roleService.updateById(data);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @DeleteMapping("/role/list/")
    public ApiResult<Role> deleteData(@RequestParam("ids") int[] ids){
        List<Integer> collect = Arrays.stream(ids).boxed().collect(Collectors.toList());
        boolean flag = roleService.removeByIds(collect);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @PutMapping("/role/status/{id}")
    public ApiResult<String> updateStatusById(@PathVariable("id") int id){
        boolean flag = baseService.updateStatusById(id);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @GetMapping("/role/list/")
    public ApiResult<List<Role>> getRoleList() {
        List<Role> list = roleService.list();
        return ApiResult.success(list);
    }

    @GetMapping("/user/role/list/{userId}")
    public ApiResult<List<Integer>> getRoleListByUserId(@PathVariable("userId") int userId){
        QueryWrapper<UserRoleRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(UserRoleRelation::getUserId, userId);
        List<UserRoleRelation> list = userRoleRelationService.list(queryWrapper);
        List<Integer> collect = list.stream().map(UserRoleRelation::getRoleId).collect(Collectors.toList());
        return ApiResult.success(collect);
    }

    @PutMapping("/role/list/allocate/")
    public ApiResult<String> allocateRoleListByUserId(@RequestParam("userId") int userId, @RequestParam("roleIds") int[] roleIds){
        List<Integer> collect = Arrays.stream(roleIds).boxed().collect(Collectors.toList());
        boolean flag = userRoleRelationService.updateUserRoleRelation(userId, collect);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

}
